/************************************************
 *** Datei: DroneNetController.h
 ***
 *** Erstellt am: 30.05.2012
 ***       Autor: Max Stark <Max.Stark88@googlemail.com>
 ***	 Version: 1.0
 ***
 *** Beschreibung:
 *** <hier Beschreibung einfuegen>
 ************************************************/

#ifndef DRONENETCONTROLLER_H_
#define DRONENETCONTROLLER_H_


#include <iostream>
#include <vector>
#include <string>
using namespace std;

#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
using namespace boost;
using namespace asio;
using namespace ip;

#define DRONENET_IPADDRESS			"192.168.2.255"
#define DRONE_BEACON_PORT 			60000
#define DRONE_MASTER_PORT			60001
#define DRONE_SLAVEDATA_PORT		60002
#define DRONE_BEACON_INTERVAL 		2000 // 2000 ms
#define DRONE_DATA_SEND_INTERVAL	20 // 20 ms
#define DRONE_MASTER_MES_REQ		"REQ"
#define DRONE_MASTER_MES_FIN		"FIN"

class DroneSwarmController;
struct DroneListEntry;

class DroneNetController {
	// Beacon Threads:
	thread* sendDroneBeaconThread;
	bool 	sendDroneBeaconThread_Active;
	void 	sendDroneBeacon();
	thread* listenDroneBeaconsThread;
	void 	listenDroneBeacons();

	// MasterMessage Threads:
	thread* listenDroneMasterMessageThread;
	bool 	listenDroneMasterMessageThread_Active;
	void 	listenDroneMasterMessage();

	// Drone Data Threads:
	thread* sendDroneSlaveDataThread;
	bool 	sendDroneSlaveDataThread_Active;
	void 	sendDroneSlaveData();
	thread* listenDroneMasterDataThread;
	void 	listenDroneMasterData();

	// Hilfsfkt.
	void	openSocket(udp::socket* socket, bool broadcast, udp::endpoint* localEndpoint);
	void 	closeSocket(udp::socket* socket);
	string 			buildDroneMessage();
	DroneListEntry 	buildDroneListEntry(char* recvBuf, string ipAddress);

	DroneSwarmController* dsController;
	bool isRunning;

public:
	DroneNetController(DroneSwarmController* dsController);
	virtual ~DroneNetController();

	void run();
	void stop();

	void sendDroneMasterMessage(string ipAddress, string mes);
};

#endif /* DRONENETCONTROLLER_H_ */
